package LCR;

public class removeNthFromEnd {

    public ListNode removeNthFromEnd2(ListNode head, int n) {
        ListNode du = new ListNode(0,head);
        ListNode slow = du;
        ListNode fast = du;
        while(n > 0) {
            fast = fast.next;
            n--;
        }
        while(fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;

        return du.next;

    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null) {
            return null;
        }

        ListNode cur = head;
        int len = 0;
        while(cur != null) {
            cur = cur.next;
            len++;
        }
        if(len == n) {
            return head.next;
        }
        len = len - n-1;
        cur = head;
        while(len > 0) {
            cur = cur.next;
            len--;
        }
        cur.next = cur.next.next;
        return head;

    }
}
